------------------------------
-- initialize our namespace --
------------------------------
FlieExample= {}
FlieExample.Scripts = {}
FlieExample.DefaultSettings = { 
	enabled = false,
}

FlieExample.Version = {
  Major     = 1,
  Minor     = 0,
  Revision  = 2,
  Build     = 1000,
}

-----------------------------------------
-- event functions via our dummy frame --
-----------------------------------------
function FlieExample.OnLoad(this)
	this:RegisterEvent("VARIABLES_LOADED")
end

function FlieExample.OnEvent(this, event)
  if (event == "VARIABLES_LOADED") then
    -- load language file
		FlieExample.locale = GetLanguage():sub(1,2):upper();
		dofile("Interface/Addons/XBar/Locales/" .. FlieExample.locale .. ".lua")

		-- if there are no settings we get from the client, use our defaults
		if not FlieExampleSettings then
			FlieExampleSettings = FlieExample.DefaultSettings;
		end

		-- tell the client to save our settings
		SaveVariables("FlieExampleSettings");

		-- register our config frame 
		FlieExample.AddonConfigTable = {
			name = "FlieExample",
			buttonTitle = "XBar Config",
			configFrame = FlieExampleConfigFrame,
			version = FlieExample.Version.Major.."."..FlieExample.Version.Minor.."."..FlieExample.Version.Revision.." b"..FlieExample.Version.Build,
			author = "Bussdee",
			fnDefault = FlieExample.Default,
			fnReadSettings = FlieExample.ReadSettings,
			fnApply = FlieExample.Apply,
		}

		LibStub("AddonConfig").RegisterConfigTable(FlieExample.AddonConfigTable);

   	-- everything else is done, so initialize up our config frame now
   	FlieExample.Initial();
  end
end

-----------------------------------------
-- init frame and apply saved settings --
-----------------------------------------
function FlieExample.Initial()
	FlieExampleConfigFrameGeneralTitle:SetText(FlieExample.localeText["GeneralTitle"]);
	FlieExampleConfigFrameGeneralCB1Desc:SetText(FlieExample.localeText["GeneralCB1Desc"]);
	FlieExampleConfigFrameGeneralCB1.OnClick = FlieExample.Scripts.GeneralCB_OnClick;
end

----------------------------------------------
-- functions that react to checkbox changes --
----------------------------------------------
function FlieExample.Scripts.GeneralCB_OnClick(this)
	local id = this:GetID();
	-- out("FlieExample: General > OnClick("..id..");");

	FlieExampleSettings.enabled = this:IsChecked();
end

--------------------------------------
-- functions to use for AddonConfig --
--------------------------------------

-- This function will be called, when the player sets the interface back
-- to the default values (button in the bottom left corner). After the defaults
-- are set our ReadSettings() will be called to apply the changes in the config
-- to the config frame.
function FlieExample.Default()
	FlieExampleSettings = FlieExample.DefaultSettings;
end

-- Here we look into our config data (FlieExampleSettings) and change the look
-- of our config page accordingly. This function will be called every time the
-- player displays our config frame.
function FlieExample.ReadSettings()
	if (FlieExampleSettings.enabled) then
		FlieExampleConfigFrameGeneralCB1:SetChecked(true);
	else
		FlieExampleConfigFrameGeneralCB1:SetChecked(false);
	end
end

-- This function will be called if the player hits the apply button, which
-- will save as well as apply all settings to the game. It will also be called
-- when the user presses the Ok button. Everything that can be changed in the
-- config frame has to be read and saved into FlieExampleSettings. Of course,
-- if the config frames makes use of functions like OnClick() for its buttons
-- to react to changes directly it does not need to do the same thing here again.
function FlieExample.Apply()
	-- [...]
end
